package com.yunwaikeji.gy_tool_library.utils;

import android.graphics.Paint;
import android.graphics.Rect;
import android.text.TextUtils;

public class GYPaintUtils {
    public static class GYPaintModel {
        private float top;
        private float ascent;
        private float descent;
        private float bottom;
        private float width;
        private float height;

        public float getTop() {
            return top;
        }

        public GYPaintModel setTop(float top) {
            this.top = top;
            return this;
        }

        public float getAscent() {
            return ascent;
        }

        public GYPaintModel setAscent(float ascent) {
            this.ascent = ascent;
            return this;
        }

        public float getDescent() {
            return descent;
        }

        public GYPaintModel setDescent(float descent) {
            this.descent = descent;
            return this;
        }

        public float getBottom() {
            return bottom;
        }

        public GYPaintModel setBottom(float bottom) {
            this.bottom = bottom;
            return this;
        }

        public float getWidth() {
            return width;
        }

        public GYPaintModel setWidth(float width) {
            this.width = width;
            return this;
        }

        public float getHeight() {
            return height;
        }

        public GYPaintModel setHeight(float height) {
            this.height = height;
            return this;
        }
    }

    public static GYTextBuilder newTextBuilder() {
        return new GYTextBuilder();
    }

    public static class GYTextBuilder {
        private String text;
        private int textSize;

        public String getText() {
            return text;
        }

        public GYTextBuilder setText(String text) {
            this.text = text;
            return this;
        }

        public int getTextSize() {
            return textSize;
        }

        public GYTextBuilder setTextSize(int textSize) {
            this.textSize = textSize;
            return this;
        }

        public GYPaintModel build() {
            Paint paint = new Paint();
            paint.setTextSize(textSize);
            Paint.FontMetrics fontMetrics = paint.getFontMetrics();
            Rect rect = new Rect();
            if (!TextUtils.isEmpty(text)) {
                paint.getTextBounds(text, 0, text.length(), rect);
            }
            GYPaintModel paintModel = new GYPaintModel();
            paintModel
                    .setTop(-fontMetrics.top)
                    .setAscent(-fontMetrics.ascent)
                    .setDescent(fontMetrics.descent)
                    .setBottom(fontMetrics.bottom)
                    .setHeight(fontMetrics.bottom - fontMetrics.top)
                    .setWidth(rect.width());
            return paintModel;
        }
    }
}
